package com.squirrel.michale;

import java.util.Arrays;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/3/8 7:15 AM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LeetCode0825 {


    public int numFriendRequests(int[] ages) {
        int n = ages.length;
        int sendCount = 0;
        Arrays.sort(ages);
        int left = 0;
        int right = 0;
        for (int age:ages) {
            if (age <= 14) {
                continue;
            }
            while (left < n && (age *0.5 + 7) >= ages[left]) {
                left++;
            }
            while (right < n && ages[right] <= age) {
                right++;
            }
            sendCount += right - left-1;
        }
        return sendCount;
    }

    public int numFriendRequests2(int[] ages) {
        int n = ages.length;
        int sendCount = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i != j) {
                    int x = ages[i];
                    int y = ages[j];
                    boolean send = true;
                    if (y > x) {
                        send = false;
                    }
                    if (y <= x / 2 + 7) {
                        send = false;
                    }
                    if (y > 100 && x < 100) {
                        send = false;
                    }
                    sendCount += send ? 1 : 0;
                }
            }
        }
        return sendCount;

    }
}
